package com.qf.purchase.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.purchase.po.*;
import com.qf.purchase.service.BrandService;
import com.qf.purchase.utils.LayUIOperate;
import com.qf.purchase.utils.TableData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Brand")
public class BrandController {
    @Resource
    private BrandService brandService;

    @RequestMapping("/getAllBrandList")
    @ResponseBody
    public TableData getAllBrandList(HttpServletRequest request) {
        int pageSize =Integer.parseInt(request.getParameter("limit"));
        int pageNumber = Integer.parseInt(request.getParameter("page"));

        String name = request.getParameter("name");
        String status = request.getParameter("status");

        Map params = new HashMap();
        params.put("name", name);
        if("0".equals(status)){
            params.put("status", null);
        }else {
            params.put("status", status);
        }

        PageHelper.startPage(pageNumber, pageSize);
        PageInfo<Brand> data = brandService.getAllBrandByPage(params);
        TableData tableData = new TableData();
        tableData.setCode(0);
        tableData.setMsg("成功");
        tableData.setCount(data.getTotal());//设置总条数
        tableData.setData(data.getList());//设置当前的数据
        return tableData;
    }

    @RequestMapping("/addBrand")
    @ResponseBody
    public LayUIOperate addBrand(@RequestBody Brand brand) {
        LayUIOperate operate=new LayUIOperate();
        boolean f= brandService.addBrand(brand);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("品牌添加成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("品牌添加失败");
        }
        return operate;
    }
    @RequestMapping("/updateBrand")
    @ResponseBody
    public LayUIOperate updateBrand(@RequestBody Brand brand) {
        LayUIOperate operate=new LayUIOperate();
        boolean f= brandService.updateBrand(brand);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("品牌修改成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("品牌修改失败");
        }
        return operate;
    }
    @RequestMapping("/updateBrandStatus")
    @ResponseBody
    public LayUIOperate updateBrandStatus(HttpServletRequest request) {
        LayUIOperate operate=new LayUIOperate();
        String id = request.getParameter("id");
        String status = request.getParameter("status");
        Brand brand = brandService.findBrandById(Integer.parseInt(id));
        brand.setStatus(status);
        boolean f= brandService.updateBrand(brand);
        if(f){
            operate.setSuccess(true);
            operate.setMessage("操作成功");
        }else{
            operate.setSuccess(false);
            operate.setMessage("操作失败");
        }
        return operate;
    }

    @RequestMapping("/findBrandById")
    @ResponseBody
    public Brand findBrandById(HttpServletRequest request) {
        String id = request.getParameter("id");
        Brand brand = brandService.findBrandById(Integer.parseInt(id));
        return brand;
    }

}
